refreshStuList();

//定义一个全局变量，用于记录当前显示的是第几页
var currentPage = 0;

//用于记录搜索条件
var searchParams;

//记录请求到的学生信息
var students;

//记录要修改的学生的id
var editID;

		
//添加学生按钮函数
$("#add-btn").click(function(){
	var params = $("#add-form").serialize();
	$.post("/stu/add",params,function(res){
		if(res.err==0){
			$("#addModal").modal("hide");
		}
		layer.msg(res.msg);
	});
});


//搜索按钮函数
$("#search-btn").click(function(){
	searchParams = $("#search-form").serialize();
	currentPage = 0;
	refreshSearch();
});


//向左翻页按钮
$("body").delegate("#left-btn","click",function(e){
	
	if($(e.currentTarget).hasClass("disabled")){
		return;
	}
	currentPage--;
	if(searchParams){
		refreshSearch();
	}else{
		refreshStuList();
	}
	
});

//向右翻页按钮
$("body").delegate("#right-btn","click",function(e){
//	console.log("右");
	if($(e.currentTarget).hasClass("disabled")){
		return;
	}
	currentPage++;
	if(searchParams){
		refreshSearch();
	}else{
		refreshStuList();
	}
});

//页码按钮
$("body").delegate(".page-btn","click",function(e){
	
	var index = $(e.currentTarget).attr("index");	
	
	currentPage = index;
	if(searchParams){
		refreshSearch();
	}else{
		refreshStuList();
	}
});

//删除按钮的函数
$("body").delegate(".delete-btn","click",function(e){
	
	layer.confirm("确定要删除吗？",{
		btn:["确定","取消"],
	},
	function(){
		var id = $(e.currentTarget).attr("sid");
		
		$.post("/stu/delete","id="+id,function(res){
			if(res.err==0){
				if(searchParams){
					refreshSearch();
				}else{
					refreshStuList();
				}
			}
			layer.msg(res.msg);
		});
		
		layer.closeAll();
	});
	
});

//修改按钮的函数
$("body").delegate(".edit-btn","click",function(e){
	
	//根据所点击这行span的索引值，从数组中找出这个学生对象
	var index = $(e.currentTarget).attr("index");
	var stu = students[index];
	
	editID = stu.id;
	
	//把学生信息添加到修改模态框中
	$("#edit-name").val(stu.name);
	$("#edit-age").val(stu.age);
	//$("#edit-"+stu.gender==1?"male":"female").prop("checked",false);
	$("#edit-"+(stu.gender==0?"male":"female")).prop("checked",true);
	$("#edit-tel").val(stu.tel);
	$("#edit-email").val(stu.email);
	$("#edit-detail").val(stu.detail);
	
	$("#editModal").modal("show");
	
});


//提交修改按钮
$("#edit-commit").click(function(){
	var params = $("#edit-form").serialize();
	
	params+= "&id="+editID;
	
	$.post("/stu/edit",params,function(res){
		if(res.err==0){
			$("#editModal").modal("hide");
			if(searchParams){
				refreshSearch();
			}else{
				refreshStuList();
			}
		}
		layer.msg(res.msg);
	});
	
});


//刷新学生列表
function refreshStuList(){
	var params = {
		page:currentPage,
		count:5
	}
//	console.dir(params);
	$.post("/stu/list",params,function(res){
		students = res.stuData;
		//渲染学生列表
		var htmlStr = template("t1",res);
		$("#stu-list").html(htmlStr);
		
		//渲染分页符
		htmlStr = template("t2",{currentPage,pages:res.pageNumber})
		$("#pager").html(htmlStr);
		
	})
}

//刷新搜索列表
function refreshSearch(){
	var param = searchParams+"&page="+currentPage;
	
	$.post("/stu/search",param,function(res){
		
		if(res.err==0){
			students = res.stuData;
			$("#searchModal").modal("hide");
			//渲染学生列表
			var htmlStr = template("t1",res);
			$("#stu-list").html(htmlStr);
			
			//渲染分页符
			htmlStr = template("t2",{currentPage,pages:res.pageNumber})
			$("#pager").html(htmlStr);
		}
	});
}


//var pages = 20;
//var arr = [];
//var temp1 = 19;
//var temp2 = 19;
//arr.push(temp1);
//while (arr.length<5){
//	temp1++;
//	if(temp1<20){
//		arr.push(temp1);
//	}
//	
//	temp2--;
//	if(temp2>=0){
//		arr.unshift(temp2);
//	}
//	
//}
//
//console.log(arr);






